//
//  emojiView.m
//  TinyFortune
//
//  Created by lc on 16/6/18.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "emojiView.h"
#define EMOJI_CODE_TO_SYMBOL(x) ((((0x808080F0 | (x & 0x3F000) >> 4) | (x & 0xFC0) << 10) | (x & 0x1C0000) << 18) | (x & 0x3F) << 24);


NSString *const cellIdentifier = @"biaoqing";

@interface emojiView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    UIPageControl *_pageController;
}

@property (strong,nonatomic)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation emojiView
#pragma mark - 初始化
- (instancetype)init{
    if (self = [super init]) {
        [self prepareData];
        [self createUI];
    }
    return self ;
}
#pragma mark - 懒加载
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray ;
}
#pragma mark - 准备表情数据
- (void)prepareData{
  for (int i=0x1F600; i<=0x1F64F; i++) {
    if (i < 0x1F641 || i > 0x1F644) {
        int sym = EMOJI_CODE_TO_SYMBOL(i);
        NSString *emoT = [[NSString alloc] initWithBytes:&sym length:sizeof(sym) encoding:NSUTF8StringEncoding];
        [self.dataArray addObject:emoT];
    }
}


}

- (void) createUI{
    self.frame = CGRectMake(0, 0, LC_WIDTH, 180);
    self.backgroundColor = [UIColor whiteColor];

    //================分页控制器===================
    _pageController = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 160, LC_WIDTH, 20)];
    _pageController.backgroundColor = [UIColor whiteColor];
    _pageController.alpha = 1;
    //设置当前分页(默认是0)
    _pageController.currentPage = 0;
    //设置选择分页的填充颜色
    _pageController.pageIndicatorTintColor = [UIColor lightGrayColor];
        //选中的分页的填充颜色
    _pageController.currentPageIndicatorTintColor = [UIColor darkGrayColor];
    //关闭用户交互
    _pageController.userInteractionEnabled = NO;
    [self addSubview:_pageController];

    
    //==================collectionView==============
    UICollectionViewFlowLayout *layout =[UICollectionViewFlowLayout new];
    //水平布局
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置每个表情按钮的大小为 30 * 30;
    layout.itemSize = CGSizeMake(30,30);
    //计算每个分区的左右边距
    float xOffset = (LC_WIDTH - 7 * 30 - 10 * 6)/2;
    //设置分区的内容偏移
    layout.sectionInset = UIEdgeInsetsMake(10, xOffset, 10, xOffset);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, LC_WIDTH, 160) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //打开分页效果
    self.collectionView.pagingEnabled = YES ;
    self.collectionView.showsHorizontalScrollIndicator = NO ;
  
    //设置列间距
    layout.minimumLineSpacing = 10 ;
    layout.minimumInteritemSpacing = 5 ;
    _collectionView.delegate = self ;
    _collectionView.dataSource = self ;
   
   [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self addSubview:_collectionView];
    //画一条线
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, LC_WIDTH, 1)];
    line.backgroundColor = [UIColor colorWithRed:239/255.0f green:239/255.0f blue:244/255.0f alpha:1];
    [self addSubview:line];
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

#pragma mark - 协议实现
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (((self.dataArray.count/28)+(self.dataArray.count%28==0?0:1))==section+1) {
         float xOffset = (LC_WIDTH - 5 * 30 - 10 * 4)/2;
        return UIEdgeInsetsMake(10, xOffset, 10, xOffset);
    }
    else{
        float xOffset = (LC_WIDTH - 7 * 30 - 10 * 6)/2;
        return UIEdgeInsetsMake(10, xOffset, 10, xOffset);
    }
}

//每页28个表情
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (((self.dataArray.count/28)+(self.dataArray.count%28==0?0:1))!=section+1) {
        return 28;
    }else{
     
        return self.dataArray.count-28*((self.dataArray.count/28)+(self.dataArray.count%28==0?0:1)-1);
    }
    
}
//返回页数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSInteger count = (self.dataArray.count/28)+(self.dataArray.count%28==0?0:1);
    _pageController.numberOfPages = count;
    return count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    for (int i =(int)cell.contentView.subviews.count ; i>0; i--) {
        [cell.contentView.subviews[i-1] removeFromSuperview];
    }
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    label.font = [UIFont systemFontOfSize:25];
    label.text =self.dataArray[indexPath.row+indexPath.section*28] ;
    
    
    [cell.contentView addSubview:label];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString * str = self.dataArray[indexPath.section*28+indexPath.row];
    //这里手动将表情符号添加到textField上
    _textView.text = [_textView.text stringByAppendingString:str];
    
    
    
}
//翻页后对分页控制器进行更新
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat contenOffset = scrollView.contentOffset.x;
    int pa = contenOffset/scrollView.frame.size.width+((int)contenOffset%(int)scrollView.frame.size.width==0?0:1);
        _pageController.currentPage = pa;
}

@end
